//
//  XGL_MessageChatVideoViewController.swift
//  XGamesLive
//
//  Created by ios on 2024/5/28.
//

import UIKit
import AudioToolbox

class XGL_MessageChatVideoViewController: UIViewController,CustomNavgationBar{
    
    var headerView: UIImageView!
    var nameText: UILabel!
    let soundId: SystemSoundID = 1005
    var header: String?
    var name: String?
    
    private var soundID: SystemSoundID = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        self.createCustomBackgroundView()
        self.createXglView()
//        self.creatClearNavigationBar(title: "")
        
        
//        
        DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
            MXProgressHUD.hudWithText("User busy") {
                AudioServicesDisposeSystemSoundID(self.soundId)
                AudioServicesRemoveSystemSoundCompletion(self.soundId)
                self.dismiss(animated: true)
            }
        }
        
        AudioServicesPlayAlertSound(self.soundId)
        AudioServicesAddSystemSoundCompletion(self.soundId, nil, nil, { id, data in
            AudioServicesPlayAlertSound(id)
        }, nil)
    }
    

}


extension XGL_MessageChatVideoViewController{
    @objc private func rejectAction(){
        AudioServicesDisposeSystemSoundID(soundId)
        AudioServicesRemoveSystemSoundCompletion(soundId)
        self.dismiss(animated: true)
    }
}

extension XGL_MessageChatVideoViewController{
    private func createXglView(){
        self.headerView = UIImageView().then({ make in
            make.layerCornerRadius = 48.0
            make.clipsToBounds = true
            make.loadImageWithUrl(self.header ?? "")
        })
        self.nameText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Semibold(fontSize: 18.0)
            make.text = self.name
        })
        let subtitleText = UILabel().then { make in
            make.text = "Calling..."
            make.textColor = .white
            make.font = UIFont.pingfangSC_Semibold(fontSize: 16.0)
        }
        let rejectButton = UIButton().then { make in
            make.setImage(imageWithName("icon_xgl_chat_video_call_reject"), for: .normal)
            make.addTarget(self, action: #selector(self.rejectAction), for: .touchUpInside)
        }
        self.view.addSubviews([self.headerView,self.nameText,subtitleText,rejectButton])
        
        self.headerView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(conversionWithScrennWidth(value: 183.0))
            make.width.height.equalTo(96.0)
        }
        self.nameText.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.headerView.snp.bottom).offset(13.0)
        }
        subtitleText.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(rejectButton.snp.top).offset(-25.0)
        }
        rejectButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(74.0)
            make.bottom.equalTo(-BottomHeight-conversionWithScrennWidth(value: 160.0))
        }
    }
}
